package com.youkeyi.ddy.cloud.application.business.hr.employee.compensation.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.compensation.dto.BusinessEmployeeCompensationListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.compensation.dto.BusinessEmployeeCompensationListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.compensation.param.BusinessEmployeeCompensationCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.compensation.param.BusinessEmployeeCompensationDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.compensation.param.BusinessEmployeeCompensationListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.compensation.param.BusinessEmployeeCompensationUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.compensation.BusinessEmployeeCompensationCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.HrEmployeeCompensationBenefitsCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.dto.HrEmployeeCompensationBenefitsListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.param.HrEmployeeCompensationBenefitsCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.param.HrEmployeeCompensationBenefitsDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.param.HrEmployeeCompensationBenefitsQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.compensation.param.HrEmployeeCompensationBenefitsUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * BusinessEmployeeCompensationController
 *
 * @Author liuxiawang
 * @Date 2024/4/29 2:14 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeeCompensationController implements BusinessEmployeeCompensationCloudService {
    
    @Autowired
    private HrEmployeeCompensationBenefitsCloudService hrEmployeeCompensationBenefitsCloudService;

    @Override
    public BusinessEmployeeCompensationListCloudDto list(BusinessEmployeeCompensationListCloudParam param) {
        BusinessEmployeeCompensationListCloudDto dto = new BusinessEmployeeCompensationListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrEmployeeCompensationBenefitsQueryCloudParam employeeQueryCloudParam = new HrEmployeeCompensationBenefitsQueryCloudParam();
        employeeQueryCloudParam.setEmployeeCompensationBenefitsPrimaryKeys(param.getEmployeeCompensationBenefitsPrimaryKeys());
        employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeQueryCloudParam.setPageSize(param.getPageSize());
        employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrEmployeeCompensationBenefitsListCloudDto employeeDtos = hrEmployeeCompensationBenefitsCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            List<BusinessEmployeeCompensationListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employee -> {
                BusinessEmployeeCompensationListItemCloudDto item = new BusinessEmployeeCompensationListItemCloudDto();
                item.setEmployeeCompensationBenefitsPrimaryKey(employee.getEmployeeCompensationBenefitsPrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                item.setSalaryAccountName(employee.getSalaryAccountName());
                item.setSalaryBank(employee.getSalaryBank());
                item.setSalaryAccountNumber(employee.getSalaryAccountNumber());
                item.setProvidentFundAccount(employee.getProvidentFundAccount());
                item.setSocialSecurityNumber(employee.getSocialSecurityNumber());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime());
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime());
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(employeeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessEmployeeCompensationCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeCompensationBenefitsCreateCloudParam employeeCreateCloudParam = new HrEmployeeCompensationBenefitsCreateCloudParam();
        employeeCreateCloudParam.setEmployeeKey(param.getEmployeeKey());
        employeeCreateCloudParam.setSalaryAccountName(param.getSalaryAccountName());
        employeeCreateCloudParam.setSalaryBank(param.getSalaryBank());
        employeeCreateCloudParam.setSalaryAccountNumber(param.getSalaryAccountNumber());
        employeeCreateCloudParam.setProvidentFundAccount(param.getProvidentFundAccount());
        employeeCreateCloudParam.setSocialSecurityNumber(param.getSocialSecurityNumber());
        employeeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeCompensationBenefitsCloudService.create(employeeCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessEmployeeCompensationUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeCompensationBenefitsPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeCompensationBenefitsUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeCompensationBenefitsUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeeCompensationBenefitsPrimaryKey(param.getEmployeeCompensationBenefitsPrimaryKey());
        employeeUpdateCloudParam.setSalaryAccountName(param.getSalaryAccountName());
        employeeUpdateCloudParam.setSalaryBank(param.getSalaryBank());
        employeeUpdateCloudParam.setSalaryAccountNumber(param.getSalaryAccountNumber());
        employeeUpdateCloudParam.setProvidentFundAccount(param.getProvidentFundAccount());
        employeeUpdateCloudParam.setSocialSecurityNumber(param.getSocialSecurityNumber());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeCompensationBenefitsCloudService.update(employeeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessEmployeeCompensationDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeCompensationBenefitsPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeCompensationBenefitsDeleteCloudParam employeeDeleteCloudParam = new HrEmployeeCompensationBenefitsDeleteCloudParam();
        employeeDeleteCloudParam.setEmployeeCompensationBenefitsPrimaryKeys(param.getEmployeeCompensationBenefitsPrimaryKeys());
        employeeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeCompensationBenefitsCloudService.delete(employeeDeleteCloudParam);
    }
}
